/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Frame_insertar.java
 *
 * Created on 5/07/2011, 12:12:42 AM
 */
package prueba_conexion;

import conexion.JmEmpleado;
import conexion.NewHibernateUtil;
import javax.swing.JOptionPane;
import javax.transaction.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Session;


/**
 *
 * @author JoseManuel
 */
public class Frame_insertar extends javax.swing.JFrame {

    /** Creates new form Frame_insertar */
    public Frame_insertar() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        B_insertaemp = new javax.swing.JButton();
        L_inserta_nomemp = new javax.swing.JLabel();
        L_inserta_apemp = new javax.swing.JLabel();
        L_inserta_diremp = new javax.swing.JLabel();
        L_inserta_telemp = new javax.swing.JLabel();
        I_inserta_nomemp = new javax.swing.JTextField();
        I_inserta_apemp = new javax.swing.JTextField();
        I_inserta_diremp = new javax.swing.JTextField();
        I_inserta_telemp = new javax.swing.JTextField();
        I_inserta_idemp = new javax.swing.JTextField();
        L_inserta_idemp = new javax.swing.JLabel();

        B_insertaemp.setText("Guardar");
        B_insertaemp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                B_insertaempActionPerformed(evt);
            }
        });

        L_inserta_nomemp.setText("Nombre");

        L_inserta_apemp.setText("Apellido");

        L_inserta_diremp.setText("Direccion");

        L_inserta_telemp.setText("Telefono");

        I_inserta_apemp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                I_inserta_apempActionPerformed(evt);
            }
        });

        I_inserta_telemp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                I_inserta_telempActionPerformed(evt);
            }
        });

        L_inserta_idemp.setText("ID");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(237, Short.MAX_VALUE)
                .addComponent(B_insertaemp)
                .addGap(92, 92, 92))
            .addGroup(layout.createSequentialGroup()
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(L_inserta_telemp)
                    .addComponent(L_inserta_diremp)
                    .addComponent(L_inserta_apemp)
                    .addComponent(L_inserta_nomemp)
                    .addComponent(L_inserta_idemp))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(I_inserta_diremp, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(I_inserta_apemp, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(I_inserta_telemp, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(I_inserta_nomemp, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(I_inserta_idemp, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(74, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(I_inserta_idemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(L_inserta_idemp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_inserta_nomemp)
                    .addComponent(I_inserta_nomemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_inserta_apemp)
                    .addComponent(I_inserta_apemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_inserta_diremp)
                    .addComponent(I_inserta_diremp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(L_inserta_telemp)
                    .addComponent(I_inserta_telemp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(B_insertaemp)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
 private void insertar(int id, String name, String lastName, String dir, String tel)
    {
        try {
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        org.hibernate.Transaction tx = session.beginTransaction();
        JmEmpleado emp = new JmEmpleado();
        emp.setIdEmpleado(id);
        emp.setNombreEmpleado(name);
        emp.setApellidoEmpleado(lastName);
        emp.setDireccionEmpleado(dir);
        emp.setTelefonoCasaEmp(tel);
        
        session.save(emp);
        tx.commit();
        JOptionPane.showMessageDialog(null,"Empleado se ha guardado correctamente");
        } catch (HibernateException he) {
            he.printStackTrace();
            JOptionPane.showMessageDialog(null,"No se ha podido Guardar");
        }
        
    }
    
    
    private void I_inserta_apempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_I_inserta_apempActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_I_inserta_apempActionPerformed

    private void I_inserta_telempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_I_inserta_telempActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_I_inserta_telempActionPerformed

    private void B_insertaempActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_B_insertaempActionPerformed
        // TODO add your handling code here:
       insertar(Integer.parseInt(I_inserta_idemp.getText()), I_inserta_nomemp.getText(),
               I_inserta_apemp.getText(),I_inserta_diremp.getText(),I_inserta_telemp.getText());
        
    }//GEN-LAST:event_B_insertaempActionPerformed

    
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame_insertar().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton B_insertaemp;
    private javax.swing.JTextField I_inserta_apemp;
    private javax.swing.JTextField I_inserta_diremp;
    private javax.swing.JTextField I_inserta_idemp;
    private javax.swing.JTextField I_inserta_nomemp;
    private javax.swing.JTextField I_inserta_telemp;
    private javax.swing.JLabel L_inserta_apemp;
    private javax.swing.JLabel L_inserta_diremp;
    private javax.swing.JLabel L_inserta_idemp;
    private javax.swing.JLabel L_inserta_nomemp;
    private javax.swing.JLabel L_inserta_telemp;
    // End of variables declaration//GEN-END:variables
}
